INTERBASE: Jak lze zjistit k dane tabulce cizi klice a odp. rodicovske tabulky ?

Otázka od: Petr Vařeka

23. 9. 2002 20:45

         tabulky ?
Zdravim a vznasim nasl. dotaz:
Pokud chci zjistit primarni klic tabulky TAB, pouzivam tuto konstrukci.

  IBTab.TableName:=TAB; // ******* Zjisteni prim. klice ********
  IBTab.IndexDefs.Update;
  Primarni_Klic:=IBTab.IndexDefs[0].Fields; // Predp. ze jde o prvni index.

Daji se nejak zjistit (treba podobne) i vsechny cizi klice k dane tabulce ?

                                                   Petr Vareka.

Odpovedá: Kalhous Zdenek

24. 9. 2002 11:21

         rodicovske tabulky ?
> Pokud chci zjistit primarni klic tabulky TAB, pouzivam tuto
> konstrukci.
> Daji se nejak zjistit (treba podobne) i vsechny cizi klice k dane
> tabulce ?
Obecne asi dost tezko uz proto, ze treba kdyz je referencni integrita
implementovana triggery tak ani v systemovych tabulkach IB neni
sance zjistit, ze nejaky sloupec je cizim klicem.

Odpovedá: Milan Tomeš

24. 9. 2002 11:03

         rodicovske tabulky ?
Snad to bude pro inspiraci stacit...

type
  TColumnRelation = record
    BaseColumn: string;
    LookupColumn: string;
  end;

  TColumnRelationsList = array of TColumnRelation;

  TRelation = record
    RelationName: string;
    LookupTableName: string;
    DetailName: string;
    ColumnRelation: TColumnRelationsList;
  end;

  TRelationsList = array of TRelation;

function Tf_Generator.getReferenceNames(
  const TableName: string): TRelationsList;
var
  Dataset: TIBDataset;
  T: TIBTransaction;
begin
  Dataset := TIBDataSet.Create(nil);
  try
    Dataset.SelectSQL.Text :=
'SELECT C.RDB$CONSTRAINT_NAME CONSTRAINT_NAME,
I1.RDB$RELATION_NAME LOOKUP_TABLE_NAME'+#13+
                              'FROM RDB$RELATION_CONSTRAINTS C'+#13+
                              'JOIN RDB$INDICES I'+#13+
                              'ON (C.RDB$INDEX_NAME =
I.RDB$INDEX_NAME)'+#13+
                              'JOIN RDB$INDICES I1'+#13+
                              
'ON (I1.RDB$INDEX_NAME =
I.RDB$FOREIGN_KEY)'+#13+
                              'WHERE'+#13+
                              
'(C.RDB$RELATION_NAME = '''+TableName+''')
AND'+#13+
                              '(C.RDB$CONSTRAINT_TYPE = ''FOREIGN KEY'')';
    T := Gor3WinApp.CreateTransactionReadOnly;
    try
      DataSet.Database := Gor3WinApp.IBDatabase;
      DataSet.Transaction := T;
      T.StartTransaction;
      Dataset.Open;
      Dataset.FetchAll;
      setLength(Result, Dataset.RecordCount);
      while not Dataset.Eof do
      begin
        Result[Dataset.RecNo - 1].RelationName :=
Trim(Dataset.fieldByName('CONSTRAINT_NAME').AsString);
        Result[Dataset.RecNo - 1].LookupTableName :=
Trim(Dataset.fieldByName('LOOKUP_TABLE_NAME').AsString);
        Dataset.Next;
      end;
      Dataset.Close;
      T.Commit;
    finally
      freeAndNil(T);
    end;
  finally
    freeAndNil(Dataset);
  end;
end;

S pozdravem

Milan Tomes
mailto:milan.tomes@haida.cz

> Daji se nejak zjistit (treba podobne) i vsechny cizi klice k dane
> tabulce ?
> Petr Vareka.